package com.person.jc.web.test.inspect;

import java.util.*;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.common.Constants;
import com.person.jc.common.FileUtil;
import com.person.jc.config.GlobalConfig;
import com.person.jc.entity.TsUser;
import com.person.jc.service.base.device.BdDeviceInfoService;
import com.person.jc.service.service.BsOrderService;
import com.person.jc.service.test.inspect.BsReconditionLogService;
import com.person.jc.service.test.inspect.BsRepairRegLogService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.JsonMapper;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;
import org.apache.commons.lang.StringUtils;

//TODO 我的检修任务
public class MyRepairController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		map.put("optUser", tsUser.getUserId());
		map.put("orderType","3");
		map.put("flow", Constants.FLOW_WX_WXFP);
		map.put("flag","repair");
		List<Record> list = BsRepairRegLogService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void viewList(){ //TODO 点击订单号查看订单里的仪器检修情况
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		map.put("userId", tsUser.getUserId());
		List<Record> list = BsReconditionLogService.me.getViewList(map);
		setAttr("list", list);
		render("viewList.jsp");
	}
	
	public void edit(){  //TODO 开始检修to edit的跳转页
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
//		map.put("orderType","2");
		List<Record> list = BsReconditionLogService.me.getViewList(map);
		List<Map<String, String>> data = new ArrayList<Map<String,String>>();
		if(list!=null && list.size()>0){
			for (Record record : list) {
				if(record.getInt("confirm_status")==0) continue;
				if(record.getStr("original_record_ok")!=null && record.getStr("original_record_ok").equals("1")) continue;
				Object recId = record.get("original_record");
				if(recId==null || !record.get("device_type").toString().equals("3")){
					Map<String, String> dataMap = new HashMap<String, String>();
					dataMap.put("name", record.getStr("device_name"));
					dataMap.put("value", record.getStr("device_no"));
					data.add(dataMap);
				}else {
//					if(record.get("device_type").equals("3")){ //设备类型为3是气检仪，气检仪才有多个原始记录单
					String gasType = record.get("gas_type").toString();
						String[] gasTypeArray = gasType.split(",");
						String[] recIdArray = recId.toString().split(",");
						int num = 0;
						for (String str : recIdArray) {
							if (StringUtils.isNotBlank(str)) {
								num++;
							}
						}
						if (num < gasTypeArray.length) {
							Map<String, String> dataMap = new HashMap<String, String>();
							dataMap.put("name", record.getStr("device_name"));
							dataMap.put("value", record.getStr("device_no"));
							data.add(dataMap);
						}

//					}
				}
			}
		}
		String resultText = JsonMapper.nonDefaultMapper().toJson(data);
		
		setAttr("inspectDate",DateUtils.getDate());
		setAttr("obj", map);
		setAttr("data", resultText);
		render("edit.jsp");
	}
	
	public void getEquipment(){ //TODO 仪器编码搜索
		String queryType = getPara("queryType");
		if(Utils.isNotEmpty(queryType)){
			String equId = getPara("equId");
			List<Record> details = BsReconditionLogService.me.getFaultPartsListBy(equId);
			renderText(JsonMapper.nonDefaultMapper().toJson(details));
		}else{
			Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
			List<Record> list = BsReconditionLogService.me.getViewList(map);
			String resultText = "none";
			if(list!=null && !list.isEmpty()){
				resultText = JsonMapper.nonDefaultMapper().toJson(list.get(0));
			}
			renderText(resultText);
		}
	}
	
	@Before(Tx.class)
	public void save(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		if(map.get("recId")==null || StringUtils.isBlank(map.get("recId").toString())) {
			TsUser tsUser = (TsUser) SessionKit.get().getAttribute(Constants.LOGIN_USER);
			map.put("optUser", tsUser.getUserId());

			//根据类型为气体并且和种类数，事先定义好original_record的路径段用，隔开
			if (map.get("deviceType").equals("3") && map.get("gasType") != null && !map.get("gasType").equals("")) {
				String[] gasTypeArray = map.get("gasType").toString().split(",");
				String str = " ";
				for (int i = 1; i < gasTypeArray.length; i++) {
					str += ", ";
				}
				map.put("originalRecord", str);
				map.put("scopes",str);

			}

			BsReconditionLogService.me.save(map);
//		if(map.get("recId")!=null && !map.get("recId").equals("")){
//
//		}else{
//
//		}
			// 设备出库验证
			String logId = (String) map.get("logId");
			BsReconditionLogService.me.partsOut(logId);

		/*
		 * 判断订单是否已检查完，检查完的更新订单FLOWID;
		 */
			//TODO 暂且关闭此时的订单flowid，等待填写原始记录单
//			String orderId = (String) map.get("orderId");
//			Integer num = BsReconditionLogService.me.getUnInspectsNum(orderId);
//			if(num == 0){
//				BsOrderService.me.updateFlow(orderId, Constants.FLOW_WX_WXRW);
//			}
		}
		renderText("success");
	}
	
	/**
	 * 维修设备检测,检查人员检修设备后的跳转。
	 */
	//TODO show原始记录单
	public void toDetect(){
		String equId = getPara("equId");
		String deviceType = getPara("deviceType");
		String orderId=getPara("orderId");
		String gasTypeId=getPara("gasTypeId");
		String sort=getPara("sort");
		Record equ = BsOrderService.me.findEquipmentById(equId);
		setAttr("equ", equ);
//		if("3".equals(deviceType)){
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("deviceType", deviceType);
//			map.put("gasType", equ.get("gas_type"));
//			List<Record> gasList = BdDeviceInfoService.me.getList(map);
//			setAttr("gasList", gasList);
//			render("detect3.jsp");
//		}else{
//		}
//		redirect("http://localhost:8080/jc/template/showTemplate");
		redirect("/template/showTemplate?id="+equId+"&deviceType="+deviceType+"&orderId="+orderId+"&gasTypeId="+gasTypeId+"&sort="+sort);
//		render("detect.jsp");

	}

	

}
